using CommonInitializer;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using System.Collections.Specialized;
using TaskMasterByQuartz.Infrastructure.Initializer;

var builder = WebApplication.CreateBuilder(args);

// DI
builder.AddCustomQuartz();

// 连接数据库的主要配置
//builder.Services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>(
//    rovider =>
//    {
//        var options = new NameValueCollection();
//        //options.Set("quartz.jobStore.clustered", 集群属性);
//        options.Set("quartz.threadPool.maxConcurrency", "100");
//        options.Set("quartz.scheduler.instanceName", "bennyScheduler");
//        options.Set("quartz.scheduler.instanceId", "AUTO");
//        options.Set("quartz.scheduler.batchTriggerAcquisitionMaxCount", "50");
//        //options.Set("quartz.jobStore.acquireTriggersWithinLock", "5");
//        options.Set("quartz.serializer.type", "json");
//        options.Set("quartz.jobStore.type", "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz");
//        options.Set("quartz.jobStore.useProperties", "false");
//        options.Set("quartz.jobStore.driverDelegateType", "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz");
//        options.Set("quartz.jobStore.tablePrefix", "QRTZ_");
//        options.Set("quartz.jobStore.dataSource", "myDS");
//        options.Set("quartz.dataSource.myDS.provider", "SqlServer");
//        options.Set("quartz.dataSource.myDS.connectionString", builder.Configuration.GetConnectionString("QuartzConnStr"));
//        return new StdSchedulerFactory(options);
//    }
//);

// 连接数据库的主要配置
builder.Services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>(
    provider =>
    {
        var config = builder.Configuration.GetSection("Quartz"); // 获取配置文件中的QuartzConfig部分
        var options = new NameValueCollection();
        // 从配置文件中获取对应的值，并设置到options中
        options.Set("quartz.scheduler.instanceName", config["quartz.scheduler.instanceName"]);
        options.Set("quartz.serializer.type", config["quartz.serializer.type"]);
        options.Set("quartz.jobStore.type", config["quartz.jobStore.type"]);
        options.Set("quartz.jobStore.useProperties", config["quartz.jobStore.useProperties"]);
        options.Set("quartz.jobStore.driverDelegateType", config["quartz.jobStore.driverDelegateType"]);
        options.Set("quartz.jobStore.tablePrefix", config["quartz.jobStore.tablePrefix"]);
        options.Set("quartz.jobStore.dataSource", config["quartz.jobStore.dataSource"]);
        options.Set("quartz.dataSource.myDS.provider", config["quartz.dataSource.myDS.provider"]);
        options.Set("quartz.dataSource.myDS.connectionString", builder.Configuration.GetConnectionString("QuartzConnStr"));
        return new StdSchedulerFactory(options);
    }
);

#region 通用配置
//builder.ConfigureDbConfiguration();
builder.ConfigureExtraServices(new InitializerOptions
{
    EventBusQueueName = "TaskMasterByQuartz.WebAPI",
    LogFilePath = "D:/Development/Project/Publish_Net/TaskMaster/temp/TaskMasterByQuartz.log"
});
#endregion

//builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
//    app.UseSwagger();
//    app.UseSwaggerUI();
//}

app.UseSwagger();
app.UseSwaggerUI();

app.UseApexDefault();
app.MapControllers();

// 配置Quartz.NET调度器初始化，这里的作用就是让项目重启的时候，继承IJob的任务类的依赖注入形式可以生效，如果不配置这些，使用了依赖注入的任务就会报错进而中断

var schedulerFactory = app.Services.GetRequiredService<ISchedulerFactory>();
var scheduler = await schedulerFactory.GetScheduler();
scheduler.JobFactory = app.Services.GetRequiredService<IJobFactory>();
await scheduler.Start();

app.Run();
